package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"yk/api/request"
	"yk/internal/dao"
	"yk/internal/model"
)

func AddCourse(c *gin.Context) {
	var req request.AddCourseReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "验证失败",
			"data": nil,
		})
		return
	}

	var video model.Video
	dao.GetOneByFields(&model.Video{
		Id: req.VideoId,
	}, &video)
	if video.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "视频不存在",
			"data": nil,
		})
		return
	}

	var course model.Course
	course = model.Course{

		VideoId:       req.VideoId,
		CourseTitle:   req.CourseTitle,
		CourseUser:    c.GetUint("userId"),
		CourseNum:     req.CourseNum,
		CourseContent: req.CourseContent,
	}

	if !dao.Create(&course) {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "课程添加失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "课程添加成功",
		"data": course,
	})

}
func ListCourse(c *gin.Context) {
	var req request.ListCourseReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "验证失败",
			"data": nil,
		})
		return
	}
	var course []model.Course
	var courses model.Course

	dao.GetListByFields(&model.Course{VideoId: req.VideoId}, &course)
	//listCourse := dao.GetListCourse(req.VideoId)
	//c.JSON(http.StatusOK, gin.H{
	//	"code": 200,
	//	"msg":  "课程信息展示成功",
	//	"data": course,
	//})

	var video []model.Video

	dao.GetListByFields(&model.Video{Id: courses.Id}, &video)
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "课程视频展示成功",
		"data": map[string]interface{}{
			"video": video,

			"course": course,
		},
	})
}
func InfoCourse(c *gin.Context) {
	var req request.InfoCourseReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "验证失败",
			"data": nil,
		})
		return
	}
	var courses model.Course

	dao.GetOneById(req.Id, &courses)
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "详情展示成功",
		"data": courses,
	})
}
